// 这份代码专门处理逻辑层的操作
const courseDao = require('../dao/courseDao');
const powerDao = require('../dao/powerDao');

const courseService = {
    /*
     * 获取推荐课程接口
     */
    async getCourse(uid, lastId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看课程');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let courseList = await courseDao.getCourse(uid, lastId);
            result = { success: true, msg: '获取推荐课程成功', data: { courseList } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 查看课程详情接口
     */
    async getCourseInfo(uid, courseId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看课程');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await courseDao.getCourseInfo(uid, courseId);
            if (data.length) {
                result = { success: true, msg: '获取课程详情成功', data: data[0] };
            } else {
                result = { success: false, msg: "获取课程详情为空" };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 购买课程接口
     */
    async buyCourse(uid, courseId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '购买课程');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await courseDao.buyCourse(uid, courseId);
            if (data.affectedRows) {
                result = { success: true, msg: '购买课程接口成功' };
            } else {
                result = { success: false, msg: "不可重复购买" };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取已购买课程列表接口
     */
    async getJoinCourse(uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看课程');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await courseDao.getJoinCourse(uid);
            if (data.length) {
                result = { success: true, msg: '获取已购买课程接口成功', data };
            } else {
                result = { success: false, msg: "该用户还未购买任何课程" };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async getAllCourse(uid, page) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '课程管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await courseDao.getAllCourse(page);
            result = { success: true, msg: '获取所有课程成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async publishCourse(uid, name, price, description, imageUrl) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '课程管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await courseDao.publishCourse(uid, name, price, description, imageUrl);
            result = { success: true, msg: '新增课程成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async deleteCourse(uid, courseId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '课程管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await courseDao.deleteCourse(uid, courseId);
            result = { success: true, msg: '删除课程成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    }
};

module.exports = courseService;